/**
 * 
 */
package com.ekingstar.framework.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.orm.hibernate3.HibernateTemplate;

import com.ekingstar.framework.domain.SystemParameter;
import com.ekingstar.framework.security.UserUtil;
import com.ekingstar.framework.service.ISystemParameterService;
import com.ekingstar.framework.util.HibernateCallbackUtil;

/**
 * 
 *
 */
public class SystemParameterServiceImpl implements ISystemParameterService {

	/* (non-Javadoc)
	 * @see com.ekingstar.framework.service.ISystemParameterService#getSystemParameterById(java.lang.String)
	 */
	private HibernateTemplate hibernateTemplate;
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
	public SystemParameter getSystemParameterById(String id) {
		// TODO Auto-generated method stub
		if (id == null || id.trim().equals("")) return null;
		SystemParameter sp  =(SystemParameter)hibernateTemplate.get(SystemParameter.class,id.trim());
		if (sp != null){
			String pId = sp.getParentId();
			if (pId != null && !pId.trim().equals("")){
				SystemParameter parent  =(SystemParameter)hibernateTemplate.get(SystemParameter.class,pId.trim());
				sp.setParent(parent);
			}
		}
		return sp;
	}
	public boolean addOrUpdateSystemParameter(SystemParameter sp) {
		if (sp == null ) return false;
		if (sp.getParentId() != null && sp.getParentId().trim().equals("")){
			sp.setParentId(null);
		}
		if (sp.getId() != null){
			SystemParameter oldSp  =(SystemParameter)hibernateTemplate.get(SystemParameter.class, sp.getId());
			oldSp.setLastUpdateDate(new Date());
			oldSp.setUpdaterId(UserUtil.getCurrentUserId());
			oldSp.setName(sp.getName());
			oldSp.setDetail(sp.getDetail());
			oldSp.setLeaf(sp.getLeaf());
			oldSp.setType(sp.getType());
			oldSp.setParentId(sp.getParentId());
			hibernateTemplate.save(oldSp);
		}
		else{
			sp.setAdderId(UserUtil.getCurrentUserId());
			sp.setAddDate(new Date());
			hibernateTemplate.save(sp);
		}
		// TODO Auto-generated method stub
		return true;
	}
	//删除时也删除子节点
	public boolean deleteSystemParameter(String sp) {
		// TODO Auto-generated method stub
		if (sp == null || sp.trim().equals("")) return false;
		SystemParameter oldSp  =(SystemParameter)hibernateTemplate.get(SystemParameter.class, sp.trim());
		Date now = new Date();
		if (oldSp != null){
			oldSp.setDeleteDate(now);
			oldSp.setDeleterId(UserUtil.getCurrentUserId());
			hibernateTemplate.save(oldSp);
			List res = hibernateTemplate.findByNamedParam(" from SystemParameter a where a.parentId=:sp and a.deleteDate is  null ","sp",sp);
			if (res != null && !res.isEmpty()){
				for (int i = 0; i < res.size();i++){
					SystemParameter tsp  = (SystemParameter)res.get(i);
					deleteSystemParameter(tsp.getId());
				}
			}
		}
		
		return true;
	}
	
	public List<SystemParameter> querySystemParameters(SystemParameter condition) {
		// TODO Auto-generated method stub
		StringBuffer sb = new StringBuffer(" from SystemParameter a where a.deleteDate is  null ");
		Map params = new HashMap();
		if (condition != null){
			if (condition.getType() != null && !condition.getType().trim().equals("")){
				sb.append(" and a.type=:type");
				params.put("type", condition.getType().trim());
			}
			if (condition.getParentId() != null && !condition.getParentId().trim().equals("")){
				if ("-1".equals(condition.getParentId().trim())){
					sb.append(" and a.parentId is null ");
				}
				else{
					sb.append(" and a.parentId=:pId");
					params.put("pId", condition.getParentId().trim());
				}
				
			}
			
		}
		List res = (List)hibernateTemplate.execute(HibernateCallbackUtil.getCallback(sb, params, false, null));
		return res;
	}
	public List getAllSystemParameters() {
		// TODO Auto-generated method stub
		List sps = hibernateTemplate.find(" from SystemParameter a where  a.deleteDate is  null ");
		return generateSystemParameterStructure(sps);
	}
	private List generateSystemParameterStructure(List sps){
		if (sps== null || sps.isEmpty() )return null;
		List res = new ArrayList();
		SystemParameter tempParent = new SystemParameter();
		for (int i = 0; i < sps.size();i++){
			SystemParameter sp = (SystemParameter)sps.get(i);
			while(true){
				if ( sp.getParentId() != null){
					SystemParameter parent = null;
					tempParent.setId(sp.getParentId());
					int index = sps.indexOf(tempParent);
					parent = (SystemParameter)sps.get(index);
					parent.getChildren().add(sp);
					sp = parent;
				}
				else{
					if (res.indexOf(sp) <0){
						res.add(sp);
					}
					break;
				}
			}
		}
		return res;
	}

}
